package com.jintoufs.service.coupon.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.coupon.CouponMapper;
import com.jintoufs.domain.coupon.Coupon;
import com.jintoufs.service.coupon.CouponService;
import com.jintoufs.util.valid.ValidUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xufei on 2017/12/20.
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Resource
    private CouponMapper couponMapper;

    @Override
    public List<Map<String,Object>> queryByParams(Map<String, Object> params) {
        return couponMapper.queryByParams(params);
    }

    @Override
    public Map<String, Object> save(Coupon coupon) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (ValidUtil.isEmpty(coupon.getId())) {
                couponMapper.insert(coupon);
                result.put("msg", "新增成功");
            } else {
                couponMapper.updateByPrimaryKeySelective(coupon);
                result.put("msg", "更新成功");
            }
            result.put("code","200");
            result.put("status", "OK");
            return result;
        } catch (Exception e) {
            result.put("code","500");
            result.put("msg", "操作失败");
            result.put("status", "ERROR");
            return result;
        }
    }

    @Override
    public void delete(Integer id) {
        couponMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Coupon> getByFilter(QueryFilters filters) {
        List<Coupon> result = couponMapper.getByFilter(filters);
        return result;
    }

    @Override
    public Coupon getById(Integer id) {
        return couponMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> selectByIds(Map<String,Object> param) {
        return couponMapper.queryByPrimaryKeys(param);
    }
}
